﻿// http://www.zimms.com
// Copyright (c) 2002-2007
// by James Zimmerman
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in the
// Software without restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
// Software, and to permit persons to whom the Software is furnished to do so, subject
// to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies
// or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

using System;
using System.Collections.Generic;
using NUnit.Framework;

[TestFixture()]
public class TypeMappingTests
{
    [Test()]
    public void ConstructorSetsProperties()
    {
        Type contractType = typeof(Object);
        Type reificationType = typeof(TypeMapping);

        TypeMapping mapping = new TypeMapping(contractType, reificationType);

        Assert.AreSame(contractType, mapping.ContractType);
        Assert.AreSame(reificationType, mapping.ReificationType);
    }

    [Test()]
    [ExpectedException(typeof(ArgumentNullException))]
    public void ContractTypeMustNotBeNull()
    {
        Type contractType = null;
        Type reificationType = typeof(TypeMapping);

        new TypeMapping(contractType, reificationType);
    }

    [Test()]
    [ExpectedException(typeof(ArgumentNullException))]
    public void ReificationTypeMustNotBeNull()
    {
        Type contractType = typeof(Object);
        Type reificationType = null;

        new TypeMapping(contractType, reificationType);
    }

    [Test()]
    [ExpectedException(typeof(ArgumentException))]
    public void ReificationTypeMustBeAssignableToContractType()
    {
        Type contractType = typeof(TypeMapping);
        Type reificationType = typeof(Object);

        new TypeMapping(contractType, reificationType);
    }

    [Test()]
    public void Equals()
    {
        TypeMapping map1 = new TypeMapping(typeof(Object), typeof(TypeMapping));
        TypeMapping map2 = new TypeMapping(typeof(Object), typeof(TypeMapping));
        TypeMapping map3 = new TypeMapping(typeof(Object), typeof(String));
        TypeMapping map4 = new TypeMapping(typeof(IEquatable), typeof(TypeMapping));


        Assert.IsFalse(map1.Equals(null));
        Assert.IsTrue(map1.Equals(map2));
        Assert.IsTrue(map1.Equals(map1));
        Assert.IsFalse(map1.Equals(map3));
        Assert.IsFalse(map1.Equals(map4));
    }
}

[TestFixture()]
public class ObjectBuilderServiceLocatorTests
{
    [Test()]
    public void CanSupplyNullEnumerator()
    {
        ObjectBuilderServiceLocator locator = new ObjectBuilderServiceLocator(null);
        locator.GetService(typeof(Object));
    }

    [Test()]
    public void CanSupplyNullEnumeratorItem()
    {
        List maps = new List();
        maps.Add(null);

        ObjectBuilderServiceLocator locator = new ObjectBuilderServiceLocator(maps);
        Object createdInstance = locator.GetService(typeof(Object));

        Assert.IsNotNull(createdInstance);
    }

    [Test()]
    public void TypeCanBeMapped()
    {
        List maps = new List();
        maps.Add(new TypeMapping(typeof(Object), typeof(ObjectBuilderServiceLocator)));

        ObjectBuilderServiceLocator locator = new ObjectBuilderServiceLocator(maps);
        Object createdInstance = locator.GetService(typeof(Object));

        Assert.IsNotNull(createdInstance);
        Assert.IsInstanceOfType(typeof(ObjectBuilderServiceLocator), createdInstance);
    }
}

